<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>权限管理</title>
    <link rel="stylesheet" th:href="@{/layuimini/lib/layui-v2.5.5/css/layui.css}"
          href="../../../static/layuimini/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" th:href="@{/layuimini/css/public.css}" href="../../../static/layuimini/css/public.css"
          media="all">
    <style>
        /*body .layui-layer-btn{
            background-color: #F8F8F8;
            border-top: 1px solid #eee;
            padding-top: 10px;
        }*/
    </style>
</head>
<body>
<div class="layuimini-container">

    <div class="layuimini-main">

        <form class="layui-form" action="" lay-filter="showForm">
            <div class="layui-form-item">

                <div class="layui-inline">
                    <div class="layui-input-block" style="margin-left: 0">
                        <input type="checkbox" checked name="switch" lay-filter="tableExpand" lay-text="全部展开|全部折叠"
                               lay-skin="switch">
                    </div>
                </div>

                <div class="layui-inline">
                    <input class="layui-input" type="text" placeholder="输入关键字">
                </div>
                <div class="layui-inline">
                    <button class="layui-btn" id="btn-search">
                        <i class="layui-icon">&#xe615;</i>搜索
                    </button>
                    <button class="layui-btn layui-btn-normal" id="btn-add">
                        <i class="layui-icon">&#xe624;</i>新增
                    </button>
                </div>
            </div>
        </form>

        <table id="munu-table" lay-filter="munu-table"></table>

    </div>

</div>
<!-- 操作列 -->
<script type="text/html" id="auth-state">
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="add">新增</a>
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script th:src="@{/layuimini/lib/layui-v2.5.5/layui.js}" src="../../../static/layuimini/lib/layui-v2.5.5/layui.js"
        charset="utf-8"></script>
<script th:src="@{/layuimini/js/lay-config.js?v=1.0.4}" src="../../../static/layuimini/js/lay-config.js?v=1.0.4"
        charset="utf-8"></script>
<script th:inline="text">
    layui.use(['table', 'treeTable', 'form', 'layer'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var treeTable = layui.treeTable;
        var form = layui.form;
        var layer = layui.layer;

        // 渲染表格
        layer.load(2);
        var tt = treeTable.render({
            elem: '#munu-table',
            url: '[[@{/}]]manage/permission/list',
            tree: {
                iconIndex: 1,
                isPidData: true,
                idName: 'authorityId',
                pidName: 'parentId',
            },
            page: false,
            cols: [
                [
                    {type: 'numbers'},
                    {field: 'authorityName', minWidth: 100, title: '权限名称'},
                    {field: 'authority', title: '权限标识'},
                    {field: 'authorityDesc', minWidth: 200, title: '权限描述'},
                    {field: 'menuUrl', title: '菜单url'},
                    // {field: 'orderNumber', width: 80, align: 'center', title: '排序号'},
                    {
                        field: 'type', width: 80, align: 'center', templet: function (d) {
                            if (d.type == 3) {
                                return '<span class="layui-badge layui-bg-gray">按钮</span>';
                            } else if (d.type == 1) {
                                return '<span class="layui-badge layui-bg-blue">模块</span>';
                            } else if (d.type == 2) {
                                return '<span class="layui-badge-rim">菜单</span>';
                            } else {
                                return '<span class="layui-badge layui-bg-green">其他</span>';
                            }
                        }, title: '类型'
                    },
                    // {templet: '#auth-state', width: 160, align: 'center', title: '操作'}
                    {
                        align: 'right', title: '操作', width: 160, templet: function (d) {
                            var btns = '<a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>\n' +
                                '    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>';
                            if (d.type<3){
                                btns='<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="add">新增</a>'+btns;
                            }
                            return btns;
                        }
                    }
                ]
            ],
            done: function () {
                layer.closeAll('loading');
            }
        });
        form.on('switch(tableExpand)', function (data) {
            if (this.checked) {
                tt.expandAll();
            } else {
                tt.foldAll();
            }
        });
        form.on("submit(showForm)", function (data) {
            return false;
        });
        $('#btn-add').click(function () {
            layer.open({
                type: 2
                , title: '添加权限资源'
                , content: '[[@{/}]]manage/permission/permission-edit.html'
                , resize: false
                , area: ['500px', '496px']
                , end:function () {
                    tt.reload();
                }
            });
        });

        // 搜索
        $('#btnSearch').click(function () {
            var keywords = $('#edtSearch').val();
            if (keywords) {
                tt.filterData(keywords);
            } else {
                tt.clearFilter();
            }
        });
        //监听工具条
        treeTable.on('tool(munu-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'del') {
                layer.confirm('删除后不可恢复，确定删除此条数据？',function (){
                    layer.load(2);
                    $.ajax({
                        type: 'get',
                        url: '[[@{/}]]manage/permission/delete',
                        data: {perId:data.authorityId},
                        dataType: 'json',
                        success: function (result) {
                            console.log(result);
                            if (result.success){
                                layer.msg("删除成功",{
                                    shift:-1,
                                    time:800
                                },function (){
                                    tt.reload();
                                });
                            }else{
                                layer.msg(result.msg);
                            }
                        },
                        error: function (jqXHR, textStatus, errorThrown) {
                            layer.msg(textStatus);
                        },
                        complete: function () {
                            layer.closeAll('loading');
                        }
                    });
                })
            } else if (layEvent === 'edit') {
                layer.open({
                    type: 2
                    , title: '修改权限资源 [id:'+data.authorityId+']'
                    , content: '[[@{/}]]manage/permission/permission-edit.html?perId='+data.authorityId+'&parentId='+data.parentId
                    , resize: false
                    , area: ['500px', '496px']
                    , end:function () {
                        tt.reload();
                    }
                });
            }else if(layEvent==='add'){
                layer.open({
                    type: 2
                    , title: '添加权限资源'
                    , content: '[[@{/}]]manage/permission/permission-edit.html?parentId='+data.authorityId
                    , resize: false
                    , area: ['500px', '496px']
                    , end:function () {
                        tt.reload();
                    }
                });
            }
        });

        form.render();
    });
</script>
</body>
</html>